package git.luohao.route;

import cn.hutool.core.util.URLUtil;
import git.luohao.mapper.RouteDefinitionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

@Component
public class StaticRouteDefinitionRepositoryImpl implements RouteDefinitionRepository {

    @Autowired
    private RouteDefinitionMapper mapper;
    @Autowired
    private RefreshRouteService service;

    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        List<LocalRoute> localRouteList = new ArrayList<>();
        //测试用，读取文件中的配置信息
//        try {
//            Scanner scanner = new Scanner(new File("D:/route.txt"));
//            while (scanner.hasNextLine()) {
//                String[] localRouteArr = scanner.nextLine().split(",");
//                localRouteList.add(new LocalRoute(localRouteArr[0], localRouteArr[1], localRouteArr[2]));
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//            return null;
//        }

//        localRouteList.add(new LocalRoute("1", "http://localhost:8088/aiplatformweb/", "/aiplatformweb/**"));
        List<RouteDefinition> routeDefinitionList = service.getAllRouteDefinition();
//        List<RouteDefinition> routeDefinitionList = new ArrayList<>();
//        for (LocalRoute localRoute : localRouteList) {
//            RouteDefinition routeDefinition = new RouteDefinition();
//            PredicateDefinition predicateDefinition = new PredicateDefinition();
//            //Route
//            routeDefinition.setId(localRoute.getId());
//            //处理api的url,拆分为uri和path两部分
//            URL url = URLUtil.url(localRoute.getUrl());
//            String uri = url.getProtocol() + "://" + url.getHost() + ":" + (url.getPort() == -1 ? url.getDefaultPort() : url.getPort());
//            String rewritePath = url.getPath();
//            try {
//                routeDefinition.setUri(new URI(uri));
//            } catch (URISyntaxException e) {
//                //如果URI格式不正确,跳过这个路由配置
//                e.printStackTrace();
//                continue;
//            }
//            //Predicate
//            String localPath = localRoute.getPath();
//            predicateDefinition.setName("Path");
//            predicateDefinition.addArg("Path", localPath);
//            routeDefinition.setPredicates(Collections.singletonList(predicateDefinition));
//            //Filter
//            List<FilterDefinition> filterDefinitionList = new ArrayList<>();
//            //判断path使用通配符情况，处理重写path配置
//            if (localRoute.getPath().endsWith("/**")) {
//                localPath = localPath.replace("/**", "/?(?<segment>.*)");
//                rewritePath = rewritePath + "/${segment}";
//            }
//            //RewritePath Filter
//            FilterDefinition rewritePathFilterDefinition = new FilterDefinition();
//            rewritePathFilterDefinition.setName("RewritePath");
//            rewritePathFilterDefinition.addArg("regexp", localPath);
//            rewritePathFilterDefinition.addArg("replacement", rewritePath);
//            filterDefinitionList.add(rewritePathFilterDefinition);
//            routeDefinition.setFilters(filterDefinitionList);
//            routeDefinitionList.add(routeDefinition);
//        }
        return Flux.fromIterable(routeDefinitionList);
    }

    @Override
    public Mono<Void> save(Mono<RouteDefinition> route) {
        return null;
    }

    @Override
    public Mono<Void> delete(Mono<String> routeId) {
        String aa = "[FilterDefinition{name='RewritePath', args={regexp=/aiplatformweb/?(?<segment>.*), replacement=/aiplatformweb//${segment}}}]";
        String predicats = "[PredicateDefinition{name='Path', args={Path=/aiplatformweb/**}}]";
        return null;
    }


}
